package com.timoon.manager.panel.product;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.ajax.markup.html.form.AjaxCheckBox;
import org.apache.wicket.ajax.markup.html.navigation.paging.AjaxPagingNavigator;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;


import com.timoon.component.panel.common.utils.AjaxBuilder;
import com.timoon.component.panel.jlayout.JLayouterPanel;
import com.timoon.manager.auth.BaseAuthPanel;
import com.timoon.sample.dao.model.Product;
import com.timoon.sample.dao.model.ProductSort;
import com.timoon.sample.utils.SystemConfig;

/**
 *<p>功能:商品栏目管理</p>
 *<p></p>
 *<p>Copyright:Copyright(c)2013</p>
 *<p>Company:sky</p>
 *@author 付乐天
 *@version 1.0 */

public class ProductTypePanel extends BaseAuthPanel {
	
	@SpringBean(name="manager.product")
	private ProductTypeService service;
	
	private String typeName;
	private Integer typeValue;
	
	private List<String> checkboxSelectedId=new ArrayList<>();

	public ProductTypePanel(String id,String typeName) {
		super(id);
		// TODO Auto-generated constructor stub
		
		this.typeName=typeName;
		
		renderPanel();
	}

	private void renderPanel() {
		// TODO Auto-generated method stub
		
		
		renderForm();
	}

	private void renderForm() {
		// TODO Auto-generated method stub
		Form form=new Form<>("wk_form");
		add(form);
		
		renderBtnList(form);
		
		renderGrid(form);
		
		renderTypeName(form);
		
		renderBtnRemove(form);
		
		renderBtnAdd(form);
	}

	private void renderBtnAdd(Form form) {
		// TODO Auto-generated method stub
		form.add(new AjaxButton("wk_add"){});
	}

	private void renderBtnRemove(Form form) {
		// TODO Auto-generated method stub
		form.add(new AjaxButton("wk_remove"){
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				// TODO Auto-generated method stub
				super.onSubmit(target, form);
				service.updateProductTypeByIds(checkboxSelectedId,Integer.valueOf(0));
				
				updateMyself(target,typeName);
			}
		});
	}

	private void renderTypeName(Form form) {
		// TODO Auto-generated method stub
		form.add(new Label("wk_type_name",this.typeName));
	}

	private void renderGrid(Form form) {
		// TODO Auto-generated method stub
		
		List<Product> products=this.service.getProductsByType(typeValue);
		
		PageableListView<Product> listView=new PageableListView<Product>("wk_list",products,10) {
			
			@Override
			protected void populateItem(ListItem<Product> arg0) {
				// TODO Auto-generated method stub
				
				final Product product=arg0.getModelObject();
				
				arg0.add(new AjaxCheckBox("wk_checkbox",new Model<Boolean>(false)) {

					@Override
					protected void onUpdate(AjaxRequestTarget arg0) {
						// TODO Auto-generated method stub
						if((boolean) this.getDefaultModelObject()){
							checkboxSelectedId.add(product.getId().toString());
						}else{
							if(checkboxSelectedId.contains(product.getId().toString())){
								checkboxSelectedId.remove(product.getId().toString());
							}
						}
						
					}
					
				});
				
				arg0.add(new Label("wk_id",product.getId()));
				
				arg0.add(new Label("wk_name",product.getName()));
				
				arg0.add(new Label("wk_market_price",product.getMarketPrice()));
				
				arg0.add(new Label("wk_actual_price",product.getActualPrice()));
				
				ProductSort sort=service.getSortByProductId(product.getSortId());
				arg0.add(new Label("wk_sort",sort.getName()));
				
			}
		};
		form.add(listView);
		
		AjaxPagingNavigator navigation=new AjaxPagingNavigator("wk_paging",listView);
		form.add(navigation);
	}

	private void renderBtnList(Form form) {
		// TODO Auto-generated method stub
		List<String> keys=new ArrayList<>(SystemConfig.PRODUCT_TYPE_MAP.keySet());
		
		//初始化typename和typevalue
		if(typeName==null){
			this.typeName=keys.get(0);
		}
		this.typeValue=SystemConfig.PRODUCT_TYPE_MAP.get(typeName);
		
		ListView<String> listView=new ListView<String>("wk_btn_list",keys) {
			
			@Override
			protected void populateItem(ListItem<String> arg0) {
				// TODO Auto-generated method stub
				final String key=arg0.getModelObject();
				
				AjaxButton btn=new AjaxButton("wk_btn") {
					@Override
					protected void onSubmit(AjaxRequestTarget target,
							Form<?> form) {
						// TODO Auto-generated method stub
						super.onSubmit(target, form);
						
						updateMyself(target,key);
					}
				};
				btn.add(new org.apache.wicket.markup.html.basic.Label("wk_btn_label",key));
				
				arg0.add(btn);
			}
		};
		form.add(listView);
	}
	
	private void updateMyself(AjaxRequestTarget target,String typeName){
		JLayouterPanel layouter=(JLayouterPanel) this.getSession().getAttribute("layouter");
		
		layouter.updatePanel(JLayouterPanel.CENTER_PANEL, new ProductTypePanel(JLayouterPanel.CENTER_PANEL,typeName), target);
	}

}
